package com.example.es.utils;

import org.elasticsearch.action.search.ClearScrollRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchScrollRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Component
public class ESUtils {
    @Resource
    private RestHighLevelClient elasticsearchClient;

    /**
     * 滚动查询全部数据
     * @param indexName 查询的索引 - 数据库
     */
    public List<String> queryDataInRange(String indexName, SearchSourceBuilder sourceBuilder) {
        List<String> results = new ArrayList<>();
        String scrollId = null;
        try {
            SearchRequest searchRequest = new SearchRequest(indexName)
                    .source(sourceBuilder)
                    .scroll(TimeValue.timeValueMinutes(1));

            // 执行搜索请求，获取第一批数据和 scrollId
            SearchResponse searchResponse = elasticsearchClient.search(searchRequest, RequestOptions.DEFAULT);
            scrollId = searchResponse.getScrollId();
            SearchHit[] searchHits = searchResponse.getHits().getHits();

            // 处理第一批数据
            results.addAll(processHits(searchHits));

            // 滚动查询处理后续的数据
            while (searchHits != null && searchHits.length > 0) {
                SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId)
                        .scroll(TimeValue.timeValueMinutes(1));
                // 滚动查询
                searchResponse = elasticsearchClient.scroll(scrollRequest, RequestOptions.DEFAULT);
                scrollId = searchResponse.getScrollId();
                searchHits = searchResponse.getHits().getHits();

                // 处理后续数据
                results.addAll(processHits(searchHits));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 清除 scroll 上下文
            if (scrollId != null) {
                ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
                clearScrollRequest.addScrollId(scrollId);
                try {
                    elasticsearchClient.clearScroll(clearScrollRequest, RequestOptions.DEFAULT);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return results;
    }

    // 处理查询到的数据
    private List<String> processHits(SearchHit[] searchHits) {
        List<String> results = new ArrayList<>();
        for (SearchHit hit : searchHits) {
            results.add(hit.getSourceAsString());
        }
        return results;
    }

}
